/*
 * CStringVector.cpp
 *
 */

#include "CStringVector.h"
#include <string.h>

char* CStringVector::end = (char*)0;

CStringVector::CStringVector() {
	vars.push_back(CStringVector::end);
}

CStringVector::~CStringVector() {
	clear();
}

CStringVector& CStringVector::pushFront(const std::string& argument) {
	char* var = new char[argument.length() + 1];
	strcpy(var, argument.c_str());
	vars.insert(vars.begin(), var);
	return *this;
}

CStringVector& CStringVector::pushBack(const std::string & argument) {
	char* var = new char[argument.length() + 1];
	strcpy(var, argument.c_str());
	vars.back() = var;
	vars.push_back(CStringVector::end);
	return *this;
}

CStringVector& CStringVector::popFront() {
	if (vars.size() > 1) {
		delete[] vars.front();
		vars.erase(vars.begin());
	}
	return *this;
}

CStringVector& CStringVector::popBack() {
	if (vars.size() > 1) {
		vars.pop_back();
		delete[] vars.back();
		vars.back() = CStringVector::end;
	}
	return *this;
}

char** CStringVector::get() {
	return vars.data();
}

const char* CStringVector::getFront() {
	return vars.front();
}

void  CStringVector::clear() {
	vars.pop_back();
	std::vector<char*>::iterator it;
	for (it = vars.begin(); it != vars.end(); ++it) {
		delete[] *it;
	}
	vars.clear();
	vars.push_back(CStringVector::end);
}

std::ostream& operator<<(std::ostream& o, const CStringVector& e) {
	std::vector<char*>::const_iterator it;
	for (it = e.vars.begin(); it != e.vars.end(); ++it) {
		if(*it != CStringVector::end)
			o << *it << '\t';
	}
	return o;
}
